package com.lhkj.ct.admin.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.admin.model.dto.OrganForm;
import com.lhkj.ct.meta.admin.model.entity.TblOrgan;
import com.lhkj.ct.meta.admin.model.vo.OrganTreeVo;
import com.lhkj.ct.meta.admin.service.SysOrganService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-09-12
 */
@RestController
@RequestMapping("api/system/organ")
@ApiSupport(order = 5)
@Api(tags = "05.机构管理")
@Validated
public class SysOrganController{

    @Resource
    private SysOrganService sysOrganService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "机构树级列表")
    @GetMapping("list")
    ////@RequiresPermissions(value = "system:organ:list")
    public ActionResult<List<OrganTreeVo>> list(TblOrgan organ, PageQuery pageQuery) {
        return ActionResult.success(sysOrganService.listOrgans(organ, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取机构下拉选项")
    @GetMapping("options")
    public ActionResult<List<Option<String>>> listDeptOptions() {
        return ActionResult.success(sysOrganService.listOptions());
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "查看", notes = "查看机构详情")
    @GetMapping("get/{id}")
    ////@RequiresPermissions(value = {"system:department:edit", "system:department:get"}, logical = Logical.OR)
    public ActionResult<?> get(@PathVariable @NotBlank(message = "id不能为空") String id) {
        TblOrgan target = sysOrganService.getById(id);
        if (target == null) {
            return ActionResult.fail("机构不存在或已删除！");
        }
        return ActionResult.success(target);
    }

    @ApiOperationSupport(order = 4)
    @PostMapping("insert")
    @ApiOperation(value = "新增机构")
    @Log(title = "机构管理", actionType = ActionType.INSERT)
    ////@RequiresPermissions(value = "system:department:insert")
    public ActionResult<Void> insert(@Validated(ValGroups.Insert.class) @RequestBody OrganForm entity) {
        return ActionResult.judge(sysOrganService.saveOrgan(entity));
    }

    @ApiOperationSupport(order = 5)
    @PutMapping("update")
    @ApiOperation(value = "编辑机构")
    @Log(title = "机构管理", actionType = ActionType.UPDATE)
    ////@RequiresPermissions(value = "system:department:edit")
    public ActionResult<Void> update(@Validated(ValGroups.Insert.class) @RequestBody OrganForm entity) {
        return ActionResult.judge(sysOrganService.saveOrgan(entity));
    }

    @ApiOperationSupport(order = 6)
    @DeleteMapping("delete")
    @ApiOperation(value = "批量删除")
    @Log(title = "机构管理", actionType = ActionType.DELETE)
    ////@RequiresPermissions(value = "system:department:delete")
    public ActionResult<Void> delete(@Validated @RequestBody @NotEmpty(message = "机构id不能为空") List<String> ids) {
        if (sysOrganService.removeByIds(ids) > 0) {
            return ActionResult.success("删除成功");
        }
        return ActionResult.fail("删除失败");
    }
}
